package uk.ac.lkl.lpp.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

import javax.swing.KeyStroke;
import javax.swing.filechooser.FileFilter;

import uk.ac.lkl.lpp.LondonPedagogyPlanner;
import uk.ac.lkl.lpp.PedagogyPlan;

import uk.ac.lkl.lpp.xml.XMLException;

public class PedagogyPlannerMenuBar extends JMenuBar {
	private static final long serialVersionUID = 1L;

    private PedagogyPlan plan;
    private LondonPedagogyPlanner lpp;
    
    public PedagogyPlannerMenuBar(LondonPedagogyPlanner lpp, PedagogyPlan plan) {
    	this.lpp = lpp;
        this.plan = plan;
        createMenuStructure();
    }


    /**
     * TODO: get menu item lables from Messages.
     * 
     */
    private void createMenuStructure() {
        //File Menu
    	JMenu fileMenu = new JMenu("File");
        fileMenu.setMnemonic(KeyEvent.VK_F);
        
        JMenu newSubMenu  = new JMenu("New");
        JMenuItem moduleMenuItem = new JMenuItem("Module",KeyEvent.VK_M);
        moduleMenuItem.addActionListener(new ActionListener() {
            	public void actionPerformed(ActionEvent e){lpp.createPlan();}
            });
        JMenuItem sessionMenuItem = new JMenuItem("Session",KeyEvent.VK_I);
        	sessionMenuItem.setEnabled(false);
        newSubMenu.add(moduleMenuItem);
        newSubMenu.add(sessionMenuItem);
        fileMenu.add(newSubMenu);
        
        JMenuItem openMenuItem = new JMenuItem("Open...", KeyEvent.VK_O);
        openMenuItem.setAccelerator(KeyStroke
                .getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
        openMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                lpp.openPlan();
            }
        });
        JMenuItem saveMenuItem = new JMenuItem("Save", KeyEvent.VK_S);
        saveMenuItem.setAccelerator(KeyStroke
                .getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
        saveMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                lpp.savePlan();
            }
        });
        JMenuItem saveAsMenuItem = new JMenuItem("Save As...", KeyEvent.VK_A);
        saveAsMenuItem.setAccelerator(KeyStroke
                .getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK));
        saveAsMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                lpp.saveAsPlan();
            }
        });
        JMenuItem exportMenuItem = new JMenuItem("Export", KeyEvent.VK_E);
        JMenuItem exitMenuItem = new JMenuItem("Exit", KeyEvent.VK_X);
        exitMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	lpp.exit();
            }
        });
        fileMenu.add(openMenuItem);
        fileMenu.add(saveMenuItem);
        fileMenu.add(saveAsMenuItem);
        fileMenu.add(exportMenuItem);
        fileMenu.add(exitMenuItem);
        this.add(fileMenu);
        
        //Edit Menu
        JMenu editMenu = new JMenu("Edit");
        editMenu.setMnemonic(KeyEvent.VK_E);
        JMenuItem copyMenuItem = new JMenuItem("Copy",KeyEvent.VK_C);
        	copyMenuItem.setEnabled(false);
        JMenuItem clearMenuItem = new JMenuItem("Clear",KeyEvent.VK_A);
        	clearMenuItem.setEnabled(false);
        this.add(editMenu);
        
        //View Menu
        JMenu viewMenu = new JMenu("View");
        	viewMenu.setEnabled(false);
        this.add(viewMenu);
        
        //Help Menu
        JMenu helpMenu = new JMenu("Help");
        helpMenu.setMnemonic(KeyEvent.VK_H);
        JMenuItem helpContentsMenuItem = new JMenuItem("Contents", KeyEvent.VK_C);
        helpContentsMenuItem.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e){
        		lpp.showHelp();
        	}
        });
        helpMenu.add(helpContentsMenuItem);

        JMenuItem relNotesMenuItem = new JMenuItem("Release notes", KeyEvent.VK_R);
        relNotesMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                lpp.showReleaseNotes();
            }
        });
        helpMenu.add(relNotesMenuItem);

        JMenuItem aboutMenuItem = new JMenuItem("About", KeyEvent.VK_A);
        aboutMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                lpp.showAbout();
            }
        });
        helpMenu.add(aboutMenuItem);
        this.add(helpMenu);
        


        exportMenuItem.addActionListener(new ActionListener(){
        	public void actionPerformed(ActionEvent e){
        		int ret=JOptionPane.OK_OPTION;
        		if(lpp.needsSave()){
        			ret = JOptionPane.showConfirmDialog(null,"File not saved","Need to save",JOptionPane.OK_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);
        		}
        		if (ret == JOptionPane.OK_OPTION){
        			// Uri: never save without asking. 
        			// add a dirty flag on the model, and check. if save needed, ask user if you should save.
            		lpp.savePlan();
            		if(lpp.getCurrentFile().getName().endsWith(".lpp")){
            			ExportDialog em= new ExportDialog(lpp.getCurrentFile(),plan);        			
            		}else{
            			File f = new File(lpp.getCurrentFile().getAbsolutePath()+".lpp");
            			// Uri: f should be a property of the class, not the chooser.
            			ExportDialog em = new ExportDialog(f,plan);   
            			
            		}
        		}
        		
        	}
        });
        
        
        editMenu.add(copyMenuItem);
        editMenu.add(clearMenuItem);
    } // createMenuStructure()

}